import React from 'react'
import { message, Space } from 'antd'
import { addWindow } from '@redux/actions'
import { awaitWrap, confirmAction, formatMoney } from '@src/assets/js/tool'
import { delRequest, generateFile } from './ajax'
import Common from '@js/common'

async function createDoc (id, callback) {
  const h = message.loading('提交中', 120)
  const [e] = await awaitWrap(generateFile(id))
  h()
  if (e === null) {
    message.success('生成成功')
    callback()
  }
}

async function delHandle (id, callback) {
  const h = message.loading('删除中', 120)
  const [e] = await awaitWrap(delRequest(id))
  h()
  if (e === null) {
    message.success('删除成功')
    callback()
  }
}

function toEdit (l, dispatch) {
  dispatch(addWindow({ id: `TJEdit-${l.id}`, title: `融入基层治理案件-${l.caseno_desc}`, componentName: 'TJCaseEdit', forceRefresh: false, meta: { id: l.id } }))
}

// 获取表格字段配置
export function getColumns (dispatch, refreshFn) {
  const c = new Common()
  return [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      width: '60px'
    },
    {
      title: '案号',
      dataIndex: 'caseno_desc',
      key: 'caseno_desc',
      width: '150px'
    },
    {
      title: '申请人',
      dataIndex: 'applicant',
      key: 'applicant',
      width: '150px'
    },
    {
      title: '被申请人',
      dataIndex: 'respondent',
      key: 'respondent',
      width: '150px'
    },
    {
      title: '案由',
      dataIndex: 'cause_action',
      key: 'cause_action',
      width: '120px'
    },
    {
      title: '和解金额',
      dataIndex: 'dispute_amount',
      key: 'dispute_amount',
      width: '120px',
      format: (v) => {
        return formatMoney(v)
      }
    },
    // {
    //   title: '仲裁调解员',
    //   dataIndex: 'arbitrator',
    //   key: 'arbitrator',
    //   width: '120px'
    // },
    {
      title: '文书',
      dataIndex: 'document',
      key: 'document',
      width: '120px',
      render: (v, l) => {
        if (v && v.id) {
          return <a onClick={() => c.$readerPdf(v.title, v.id, l.caseid)}>{v.title}</a>
        }
      }
    },
    {
      title: '操作',
      dataIndex: 'id',
      key: 'id',
      width: '150px',
      render: (v, l) => {
        return (
          <Space>
            <a onClick={() => { toEdit(l, dispatch) }}>编辑</a>
            <a onClick={() => { confirmAction('您确认要重新生成文书吗？', () => createDoc(l.id, refreshFn)) }}>重新生成文书</a>
            <a onClick={() => { confirmAction(`您确定要删除 ${l.caseno_desc || l.id} 吗？`, () => { delHandle(l.id, refreshFn) }) }}>删除</a>
          </Space>
        )
      }
    }
  ]
}
